
const SERVICE = require("../../lib/js/service.js");

Page({
  data:{
    imgSrc: SERVICE.imgUrl,
    showNoData: false
  },
  onLoad(){
    this.getOrderList();
  },

  /*分享*/
  onShareAppMessage(){

  },

  /**
   * @explain getOrderList 我的订单列表
   * 
   * */
  getOrderList(){
    wx.showNavigationBarLoading();
    let _this = this;
    SERVICE.POST(SERVICE.URL.orderList,{
      thirdCookie: wx.getStorageSync("thirdCookie")
    },(status,res)=>{
     wx.hideNavigationBarLoading();
     if(res.data.status === "success"){
       let list = res.data.result;
       if(list.length > 0){
         for (let i = 0; i < list.length; i++) {
           list[i].selected = false;
         }
         _this.setData({
           list
         });
       }else{
         _this.setData({
           showNoData: true,
           list: null
         });
       }
     }else{
       wx.showToast({
         title: "获取订单失败",
         icon: "none",
         mask: true,
         success:function(){
           _this.setData({
             showNoData: true
           });
         }
       });
     }
    });
  },

  /**
   * @explain showDetail 显示 隐藏 详情
   * 
   * */ 
  showDetail(e){
    let td = this.data, index = e.currentTarget.dataset.index;
    for (let i = 0; i < td.list.length; i++){
      if(i === index){
        td.list[i].selected = !td.list[i].selected;
      }
    }
    this.setData({
      list: td.list
    });
  },

  /**
   * @explain 删除
   * @param  thirdCookie 用户标识
   * @param  id_list 删除的数组id（JSON）
   * 
   * **/
  deleteShopCar(e) {
    let _this = this,
      delete_shop = [],
      arr = e.currentTarget.dataset.arr;
      console.log(arr);
    for (let i = 0; i < arr.length; i++) {
      delete_shop.push(arr[i].id);
    }
    wx.showModal({
      title: "提示",
      content: "确定要删除吗",
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: "删除中...",
            mask: true
          });
          SERVICE.POST(SERVICE.URL.deleteShopCar, {
            thirdCookie: wx.getStorageSync("thirdCookie"),
            id_list: JSON.stringify(delete_shop)
          }, (status, res) => {
            wx.hideLoading();
            if (res.data.status == "success") {
              _this.getOrderList();
            } else {
              wx.showToast({
                title: "删除失败",
                icon: "none",
                mask: true
              });
            }
          });
        }
      }
    })
  },
  
});